/*
 * uartSample.c
 *
 * This file contains the test / demo code to demonstrate the UART driver functi-
 * onality in Interrupt mode of operation on DSP/BIOS 5.

*/

/** \file   uartSample.c
 */

#include <std.h>
#include <stdio.h>
#include <string.h>
#include <gio.h>
#include <log.h>
#include <tsk.h>
#include <stdio.h>

#include "ti/pspiom/uart/Uart.h"
#include "ti/pspiom/psc/Psc.h"
#include "ti/pspiom/platforms/evmOMAPL137/Uart_evmInit.h"
/*
 * External references
 */
extern LOG_Obj    trace;

/*
 * UART0 device params. To be filled in uart0_dev_init function which
 * is called before driver creation
 */
Uart_Params   uartParams;

/*
 * Starting message printing string
 * Note: Buffer alignement is required only when working in DMA Mode.
 */
#pragma DATA_ALIGN(uartTestString, 128);
#define UARTESTSTRING_LEN 128
static char uartTestString[UARTESTSTRING_LEN];

/* Tx memory buffer */
#pragma DATA_ALIGN(TxDataBuf, 128);
#define TXDATABUF_LEN 12
static	char		TxDataBuf[TXDATABUF_LEN];
		char* 		TxData = TxDataBuf;


/* UART handle for input channel */
GIO_Handle hUart_IN;

/* UART handle for output channel */
GIO_Handle hUart_OUT;

/* Local function prototypes */
static void genericUartTx(char* TxData);

/* ========================================================================== */
/*                        FUNCTION DEFINTIONS                                 */
/* ========================================================================== */



Void Uart_Tx_Task(Void)
{
    GIO_Attrs gioAttrs = GIO_ATTRS;
    Int32     		echoTskStatus = 0;
	Uint32			timestamp = 0;
	Int16			i = 0;

    /* Initialize channel attributes.                                         */
    gioAttrs.nPackets = 2;

    /* Initialize pinmux and evm related configurations                       */
    configureUart();

   /* Initialize UART Currently is been used to display a string              */
    hUart_OUT = GIO_create("/UART0",IOM_OUTPUT,NULL,NULL,&gioAttrs);
    hUart_IN  = GIO_create("/UART0",IOM_INPUT,&echoTskStatus,NULL,&gioAttrs);

    if ((NULL == hUart_IN) || (NULL == hUart_OUT))
    {
        LOG_printf(&trace, "ERROR: Initialization of UART failed\n");
        return;
    }

    if ((NULL != hUart_IN) && (NULL != hUart_OUT))
    {

		for (i = 0; i < UARTESTSTRING_LEN ; i++) uartTestString[i] = 0x00; //ripulisco UartTestString

		while(1) {
	        /* INVIO TIMESTAMP DI QUANDO IN QUANDO */
/*			for (i = 0; i < TXDATABUF_LEN; i++) TxDataBuf[i] = 0x00;
			sprintf ( TxData, "%u", timestamp ); //convert ul to string
	        genericUartTx(TxData);*/
			TSK_sleep(500);
			timestamp++;
			
		
		}


    }

    return;
}


/**
 * \brief    Initialisation function.
 *           This function initializes the UART driver and also the required
 *           parameters for the creation of the device.
 */
Void user_uart0_init(Void)
{
    Uart_init();
    uartParams = Uart_PARAMS;
    uartParams.hwiNumber = 9;
    uartParams.opMode = Uart_OpMode_INTERRUPT;
    uartParams.rxThreshold = Uart_RxTrigLvl_1;
}

/*
 * genericUartTx( ... )
 *
 *
 *
 */


static Void genericUartTx(char* TxData)
{
    Int     status = 0;
    size_t  len    = 0;
    Int8   *str    = NULL;

    LOG_printf(&trace," Sending Tx timestamp \n\r");

    strcat(TxData,", "); //numero e virgola per mandare in output un CSV file

    /* Copy to start string to Cache aligned buffer                           */
    len = strlen(TxData); 
    memcpy(uartTestString,str,len);

    status = GIO_submit(hUart_OUT,IOM_WRITE, TxData, &len, NULL);

    if(!((status == IOM_COMPLETED)||(status == IOM_PENDING)))
    {
        LOG_printf(&trace, "\r\n Error from GIO_write for UART Test string\n");
    }
	return;
}


/* ========================================================================== */
/*                              END OF FILE                                   */
/* ========================================================================== */
